package com.example.hasee.plant.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ActionMode;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.plant.R;
import com.example.hasee.plant.config.MSharePreference;
import com.example.hasee.plant.config.MyApplication;
import com.example.hasee.plant.fragment.GatherImageFragment;
import com.example.hasee.plant.fragment.GatherRecordFragment;
import com.example.hasee.plant.fragment.GatherVideoFragment;
import com.example.hasee.plant.model.SaveDataModel;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hasee on 2017-01-31.
 */

public class GatherDataActivity extends FragmentActivity implements View.OnClickListener{

    String num;
    String videopath;
    Bitmap bitmap=null;
    private List<Fragment> fragmentList;
    private FragmentPagerAdapter mAdapter;
    private SaveDataModel dataModel;
    private MSharePreference sharePreference;

    private android.support.v4.view.ViewPager viewpager;
    private android.widget.TextView tvimg;
    private android.widget.TextView tvrecord;
    private android.widget.TextView tvvideo;
    private TextView tvSave;
    private TextView tvDataList;

    private boolean isPressSave=false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gather_data_activity);
        sharePreference=new MSharePreference(getApplicationContext());
        this.tvvideo = (TextView) findViewById(R.id.tv_video);
        this.tvrecord = (TextView) findViewById(R.id.tv_record);
        this.tvimg = (TextView) findViewById(R.id.tv_img);
        this.viewpager = (ViewPager) findViewById(R.id.viewpager);
        tvSave= (TextView) findViewById(R.id.tvSave);
        tvDataList= (TextView) findViewById(R.id.tvDataList);

        fragmentList=new ArrayList<>();
        videopath= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recordtest/20172210557.mp4";

        onListener();
        setFragment();
        setViewPager();
        tvimg.performClick();

    }

    private void setFragment(){
        fragmentList.add(new GatherImageFragment());
        fragmentList.add(new GatherRecordFragment());
        fragmentList.add(new GatherVideoFragment());
    }


    private void setViewPager(){

        mAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewpager.setAdapter(mAdapter);
        viewpager.setOffscreenPageLimit(2);

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                resetBtn();
                switch (position){
                    case 0:
                        tvimg.setTextColor(getResources().getColor(R.color.blue));
                        break;
                    case 1:
                        tvrecord.setTextColor(getResources().getColor(R.color.blue));
                        break;
                    case 2:
                        tvvideo.setTextColor(getResources().getColor(R.color.blue));
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void resetBtn(){
        tvimg.setTextColor(getResources().getColor(R.color.gray));
        tvrecord.setTextColor(getResources().getColor(R.color.gray));
        tvvideo.setTextColor(getResources().getColor(R.color.gray));
    }

    private void onListener(){
        tvimg.setOnClickListener(this);
        tvrecord.setOnClickListener(this);
        tvvideo.setOnClickListener(this);
        tvSave.setOnClickListener(this);
        tvDataList.setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(!isPressSave){
            File imgFile=new File(MyApplication.IMG_PATH_FINISH);
            File audioFile=new File(MyApplication.AUDIO_PATH_FINISH);
            File videoFile=new File(MyApplication.VIDEO_PATH_FINISH);
            if(imgFile.exists()){
                imgFile.delete();
            }
            if(audioFile.exists()){
                audioFile.delete();
            }
            if(videoFile.exists()){
                videoFile.delete();
            }
        }

        MyApplication.VIDEO_PATH_FINISH=null;
        MyApplication.AUDIO_PATH_FINISH=null;
        MyApplication.IMG_PATH_FINISH=null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_img:
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv_record:
                viewpager.setCurrentItem(1);
                break;
            case R.id.tv_video:
                viewpager.setCurrentItem(2);
                break;
            case R.id.tvSave:
                isPressSave=true;
                dataModel=new SaveDataModel();
                dataModel.setNum(MyApplication.GATHER_NUM);
                dataModel.setImgPath(MyApplication.IMG_PATH_FINISH);
                dataModel.setRecordPath(MyApplication.AUDIO_PATH_FINISH);
                dataModel.setVideoPath(MyApplication.VIDEO_PATH_FINISH);
                Gson gson=new Gson();
                String jsonString=gson.toJson(dataModel);
                Log.d("datamap","jsonString:"+jsonString);
                MSharePreference.putString(getApplicationContext(),MyApplication.GATHER_NUM,jsonString);
//                sharePreference.putString(num,jsonString);
                Toast.makeText(GatherDataActivity.this,"保存数据成功",Toast.LENGTH_SHORT).show();

                break;
            case R.id.tvDataList:
                Intent intent=new Intent(GatherDataActivity.this,DataListActivity.class);
                startActivity(intent);
                break;
        }
    }


}
